<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>curl常用命令 | 若愚湖畔</title><meta name="keywords" content="杂项"><meta name="author" content="albafica"><meta name="copyright" content="albafica"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="简介curl是常用的命令行工具，用来请求web服务器，他的名字就是客户端(client)的的url工具的意思。借助这个工具，可以在无法使用postman等工具情况下或者构造简单命令请看下可以非常快捷的实现http请求。 常用参数-A-A 参数用于指定客户端用户代理标识，即User-Agent. 1curl -A &quot;custom&#x2F;1.0&quot; http:&#x2F;&#x2F;www.baidu.com">
<meta property="og:type" content="article">
<meta property="og:title" content="curl常用命令">
<meta property="og:url" content="https://albafica.gitee.io/2022/04/27/curl/index.html">
<meta property="og:site_name" content="若愚湖畔">
<meta property="og:description" content="简介curl是常用的命令行工具，用来请求web服务器，他的名字就是客户端(client)的的url工具的意思。借助这个工具，可以在无法使用postman等工具情况下或者构造简单命令请看下可以非常快捷的实现http请求。 常用参数-A-A 参数用于指定客户端用户代理标识，即User-Agent. 1curl -A &quot;custom&#x2F;1.0&quot; http:&#x2F;&#x2F;www.baidu.com">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="">
<meta property="article:published_time" content="2022-04-27T06:01:53.000Z">
<meta property="article:modified_time" content="2022-04-27T06:32:14.100Z">
<meta property="article:author" content="albafica">
<meta property="article:tag" content="杂项">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content=""><link rel="shortcut icon" href="/hexoblog/img/favicon.png"><link rel="canonical" href="https://albafica.gitee.io/2022/04/27/curl/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/hexoblog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/hexoblog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'curl常用命令',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-04-27 14:32:14'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.1.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://s1.ax1x.com/2022/04/23/Lhpz3F.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/hexoblog/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="data-item"><a href="/hexoblog/tags/"><div class="headline">标签</div><div class="length-num">6</div></a></div><div class="data-item"><a href="/hexoblog/categories/"><div class="headline">分类</div><div class="length-num">2</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/hexoblog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/hexoblog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/hexoblog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/hexoblog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/hexoblog/">若愚湖畔</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/hexoblog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/hexoblog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/hexoblog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/hexoblog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">curl常用命令</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-04-27T06:01:53.000Z" title="发表于 2022-04-27 14:01:53">2022-04-27</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-04-27T06:32:14.100Z" title="更新于 2022-04-27 14:32:14">2022-04-27</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">415</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>1分钟</span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h1><p>curl是常用的命令行工具，用来请求web服务器，他的名字就是客户端(client)的的url工具的意思。<br>借助这个工具，可以在无法使用postman等工具情况下或者构造简单命令请看下可以非常快捷的实现http请求。</p>
<h1 id="常用参数"><a href="#常用参数" class="headerlink" title="常用参数"></a>常用参数</h1><h2 id="A"><a href="#A" class="headerlink" title="-A"></a>-A</h2><p><code>-A</code> 参数用于指定客户端用户代理标识，即<code>User-Agent</code>.</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl -A &quot;custom/1.0&quot; http://www.baidu.com</span><br></pre></td></tr></table></figure>


<h2 id="H"><a href="#H" class="headerlink" title="-H"></a>-H</h2><p><code>-H</code> 用于指定请求的header头，比如除了通过<code>-A</code>指定<code>User-Agent</code>外，还可以通过这个参数指定</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl -H &#x27;User-Agent: custom/1.0&#x27; http://www.baidu.com</span><br></pre></td></tr></table></figure>

<h2 id="d"><a href="#d" class="headerlink" title="-d"></a>-d</h2><p><code>-d</code> 参数用于发送POST请求的数据体</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">curl -d&#x27;login=emma＆password=123&#x27;-X POST https://google.com/login</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">或者</span></span><br><span class="line">curl -d &#x27;login=emma&#x27; -d &#x27;password=123&#x27; -X POST  https://google.com/login</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">也可以读取本地文件</span></span><br><span class="line">curl -d &#x27;@data.txt&#x27; https://google.com/login</span><br></pre></td></tr></table></figure>

<h2 id="i"><a href="#i" class="headerlink" title="-i"></a>-i</h2><p><code>-i</code> 参数会打印出服务器响应的http标头,空一行后再输出响应body</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl -i http://www.baidu.com</span><br></pre></td></tr></table></figure>

<h2 id="I"><a href="#I" class="headerlink" title="-I"></a>-I</h2><p><code>-I</code> 参数会发出<code>HEAD</code>请求，即只获取http的响应头信息</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl -I http://www.baidu.com</span><br></pre></td></tr></table></figure>

<h2 id="L"><a href="#L" class="headerlink" title="-L"></a>-L</h2><p><code>-L</code> 会让http请求跟谁服务器的重定向。 curl默认不会跟随重定向</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl -L http://www.51jingying.com</span><br></pre></td></tr></table></figure>

<h2 id="o"><a href="#o" class="headerlink" title="-o"></a>-o</h2><p><code>-o</code>参数会将服务器回应保存成文件，等同于wget命令</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl -o job.html https://arts.51job.com/arts/05/452881.html</span><br></pre></td></tr></table></figure>

<h2 id="O"><a href="#O" class="headerlink" title="-O"></a>-O</h2><p><code>-P</code> 参数会将服务器回应保存为文件，并将url最后一部分当成文件名</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl -O https://arts.51job.com/arts/05/452881.html</span><br></pre></td></tr></table></figure>
<h2 id="v"><a href="#v" class="headerlink" title="-v"></a>-v</h2><p><code>-v</code>参数输出整个通信过程用于调试</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl -v http://www.baidu.com</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">albafica</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://albafica.gitee.io/2022/04/27/curl/">https://albafica.gitee.io/2022/04/27/curl/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://albafica.gitee.io" target="_blank">若愚湖畔</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/hexoblog/tags/%E6%9D%82%E9%A1%B9/">杂项</a></div><div class="post_share"></div></div><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/hexoblog/2022/04/23/install-hexo/" title="hexo 安装说明"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-04-23</div><div class="title">hexo 安装说明</div></div></a></div><div><a href="/hexoblog/2022/05/18/techupdate/" title="技术升级简要构思"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-18</div><div class="title">技术升级简要构思</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://s1.ax1x.com/2022/04/23/Lhpz3F.png" onerror="this.onerror=null;this.src='/hexoblog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">albafica</div><div class="author-info__description">Stay hungry Stay foolish</div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/hexoblog/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="card-info-data-item"><a href="/hexoblog/tags/"><div class="headline">标签</div><div class="length-num">6</div></a></div><div class="card-info-data-item"><a href="/hexoblog/categories/"><div class="headline">分类</div><div class="length-num">2</div></a></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AE%80%E4%BB%8B"><span class="toc-number">1.</span> <span class="toc-text">简介</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E5%8F%82%E6%95%B0"><span class="toc-number">2.</span> <span class="toc-text">常用参数</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#A"><span class="toc-number">2.1.</span> <span class="toc-text">-A</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#H"><span class="toc-number">2.2.</span> <span class="toc-text">-H</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#d"><span class="toc-number">2.3.</span> <span class="toc-text">-d</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#i"><span class="toc-number">2.4.</span> <span class="toc-text">-i</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#I"><span class="toc-number">2.5.</span> <span class="toc-text">-I</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#L"><span class="toc-number">2.6.</span> <span class="toc-text">-L</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#o"><span class="toc-number">2.7.</span> <span class="toc-text">-o</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#O"><span class="toc-number">2.8.</span> <span class="toc-text">-O</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#v"><span class="toc-number">2.9.</span> <span class="toc-text">-v</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/18/techupdate/" title="技术升级简要构思">技术升级简要构思</a><time datetime="2022-05-18T12:54:43.000Z" title="发表于 2022-05-18 20:54:43">2022-05-18</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/07/functionalinterface/" title="Function接口简单代码结构">Function接口简单代码结构</a><time datetime="2022-05-07T10:18:48.000Z" title="发表于 2022-05-07 18:18:48">2022-05-07</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/07/cap/" title="微服务-CAP理论">微服务-CAP理论</a><time datetime="2022-05-07T01:12:07.000Z" title="发表于 2022-05-07 09:12:07">2022-05-07</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/02/optional/" title="optional的使用">optional的使用</a><time datetime="2022-05-02T02:10:03.000Z" title="发表于 2022-05-02 10:10:03">2022-05-02</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/01/pattern-overload/" title="设计模式-概述">设计模式-概述</a><time datetime="2022-05-01T10:12:20.000Z" title="发表于 2022-05-01 18:12:20">2022-05-01</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By albafica</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/hexoblog/js/utils.js"></script><script src="/hexoblog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><div class="js-pjax"></div><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="富强,民主,文明,和谐,爱国,敬业,诚信,友善" data-fontsize="15px" data-random="false" async="async"></script></div></body></html>